package com.medical.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.medical.DTO.MedicalRecordsDTO;
import com.medical.DTO.MedicalRecordsPageDTO;
import com.medical.Entity.MedicalRecords;
import com.medical.Mapper.MedicalRecordsMapper;
import com.medical.Service.MedicalRecordsService;
import com.medical.VO.MedicalRecordsVO;
import com.medical.result.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MedicalRecordsServiceImpl implements MedicalRecordsService {
    @Autowired
    private MedicalRecordsMapper medicalRecordsMapper;

    /*
     * 添加医疗记录
     */
    @Override
    public void addMedicalRecords(MedicalRecordsDTO medicalRecordsDTO) {
        MedicalRecords medicalRecords = new MedicalRecords();
        BeanUtils.copyProperties(medicalRecordsDTO, medicalRecords);

        medicalRecords.setCreatedDate(LocalDateTime.now());
        medicalRecords.setRecordDate(LocalDate.now());
        medicalRecordsMapper.addMedicalRecords(medicalRecords);
    }

    /*
     * 删除医疗记录
     */
    @Override
    public void deleteMedicalRecords(List<Integer> ids) {
        medicalRecordsMapper.delepteMedicalRecords(ids);
    }

    /*
     * 修改医疗记录
     */
    @Override
    public void updateMedicalRecords(MedicalRecordsDTO medicalRecordsDTO) {
        MedicalRecords medicalRecords = new MedicalRecords();
        BeanUtils.copyProperties(medicalRecordsDTO, medicalRecords);
        medicalRecords.setRecordDate(LocalDate.now());
        medicalRecordsMapper.updateMedicalRecords(medicalRecords);
    }

    @Override
    public PageResult page(MedicalRecordsPageDTO medicalRecordsPageDTO) {
        PageHelper.startPage(medicalRecordsPageDTO.getPage(), medicalRecordsPageDTO.getPageSize());
        Page<MedicalRecordsVO> page = medicalRecordsMapper.pageSelect(medicalRecordsPageDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }
}
